<template>
  <div class="wrap">
    <div style="margin-bottom:10px;">
      <el-date-picker
        :editable="false"
        :picker-options="pickerOptions"
        value-format="yyyy-MM-dd"
        v-model="dateValue"
        type="daterange"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        @change="dateChange">
      </el-date-picker>
      <!-- <el-select v-model="systemValue" placeholder="系统" class="select" style="width:150px;" @change="systemChange">
        <el-option
          v-for="item in systemOptions"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select> -->
      <el-switch
        v-model="newUserSwitch"
        active-text="仅看新用户"
        @change="newUserSwitchChange">
      </el-switch>
      <el-button icon="el-icon-refresh" @click="refreshClick" style="margin-left:20px;">刷新</el-button>
    </div>
    <el-table
      class="table"
      border fit highlight-current-row
      v-loading="loading"
      element-loading-text="加载中"
      element-loading-spinner="el-icon-loading"
      :data="tableData"
      style="width: 100%">
      <el-table-column
          align="center"
          type="index"
          width="50">
      </el-table-column>
      <el-table-column
          align="center"
          width="100"
          label="日期">
          <template slot-scope="scope">
            <p>{{_mapping.date_format2(scope.row.createTime)}}</p>
          </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="`APP登录|APP激活 ${this.tab.appLoginNumTotal || 0} | ${this.tab.appActiveNumTotal || 0}`"
        :render-header="renderHeader2">
        <template slot-scope="scope">
          <span>{{scope.row.appLoginNum || 0}} | {{scope.row.appActivationNum || 0}}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="`产品用户数|比率 ${this.tab.productUserNumTotal || 0} | ${this.tab.productUserRateAve ? this.tab.productUserRateAve+'%' : '-'}`"
        :render-header="renderHeader2">
        <template slot-scope="scope">
          <span>{{scope.row.productUserNum || 0}} | {{isNaN((scope.row.productUserNum*100/scope.row.appLoginNum).toFixed(1)) ? '-' : ((scope.row.productUserNum*100/scope.row.appLoginNum).toFixed(1))+'%'}}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="`产品PV数|重复率 ${this.tab.productPvNumTotal || 0} | ${this.tab.chongfuRateAve ? this.tab.chongfuRateAve+'%' : '-'}`"
        :render-header="renderHeader2">
        <template slot-scope="scope">
          <span>{{scope.row.productPvNum || 0}} | {{isNaN((scope.row.productPvNum*100/scope.row.productUvNum).toFixed(1)) ? '-' : ((scope.row.productPvNum*100/scope.row.productUvNum).toFixed(1))+'%'}}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="`产品UV数|比率|总比率 ${this.tab.productUvNumTotal || 0} | ${this.tab.productUvRateAve ? this.tab.productUvRateAve+'%' : '-'} | ${this.tab.productUvRateTotal ? this.tab.productUvRateTotal+'%' : '-'}`"
        :render-header="renderHeader3">
        <template slot-scope="scope">
          <span>{{scope.row.productUvNum || 0}} | {{isNaN((scope.row.productUvNum*100/scope.row.productUserNum).toFixed(1)) ? '-' : ((scope.row.productUvNum*100/scope.row.productUserNum).toFixed(1))+'%'}} | {{isNaN((scope.row.productUvNum*100/scope.row.appLoginNum).toFixed(1)) ? '-' : ((scope.row.productUvNum*100/scope.row.appLoginNum).toFixed(1))+'%'}}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="`预测注册|比率|总比率 ${this.tab.regisNumTotal || 0} | ${this.tab.regisRateAve ? this.tab.regisRateAve+'%' : '-'} | ${this.tab.regisRateTotal ? this.tab.regisRateTotal+'%' : '-'}`"
        :render-header="renderHeader3">
        <template slot-scope="scope">
          <span>{{scope.row.cpaNum || 0}} | {{isNaN((scope.row.cpaNum*100/scope.row.productUvNum).toFixed(1)) ? '-' : ((scope.row.cpaNum*100/scope.row.productUvNum).toFixed(1))+'%'}} | {{isNaN((scope.row.cpaNum*100/scope.row.appLoginNum).toFixed(1)) ? '-' : ((scope.row.cpaNum*100/scope.row.appLoginNum).toFixed(1))+'%'}}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="`贷款罗盘|比率 ${this.tab.luopanNumTotal || 0} | ${this.tab.luopanRateAve ? this.tab.luopanRateAve+'%' : '-'}`"
        :render-header="renderHeader2">
        <template slot-scope="scope">
          <span>{{scope.row.listUvNum || 0}} | {{isNaN((scope.row.listUvNum*100/scope.row.appLoginNum).toFixed(1)) ? '-' : ((scope.row.listUvNum*100/scope.row.appLoginNum).toFixed(1))+'%'}}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="`必下款|比率 ${this.tab.bixiakuanNumTotal || 0} | ${this.tab.bixiakuanRateAve ? this.tab.bixiakuanRateAve+'%' : '-'}`"
        :render-header="renderHeader2">
        <template slot-scope="scope">
          <span>{{scope.row.mustUvNum || 0}} | {{isNaN((scope.row.mustUvNum*100/scope.row.appLoginNum).toFixed(1)) ? '-' : ((scope.row.mustUvNum*100/scope.row.appLoginNum).toFixed(1))+'%'}}</span>
        </template>
      </el-table-column>
       <el-table-column
        align="center"
        :label="`签到抢红包|比率 ${this.tab.redNumTotal || 0} | ${this.tab.redRateAve ? this.tab.redRateAve+'%' : '-'}`"
        :render-header="renderHeader2">
        <template slot-scope="scope">
          <span>{{scope.row.redUvNum || 0}} | {{isNaN((scope.row.redUvNum*100/scope.row.appLoginNum).toFixed(1)) ? '-' : ((scope.row.redUvNum*100/scope.row.appLoginNum).toFixed(1))+'%'}}</span>
        </template>
      </el-table-column>
    </el-table>
    <div class="clearfix pagination" style="margin-top:30px;">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="query.pageNum"
        :page-size="query.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalNum">
      </el-pagination>
    </div>
  </div>
</template>
<script>
import _statistics from '@/services/statistics.js'
import _mapping from '@/utils/_mapping.js'
export default {
  data() {
    return {
      query: {
        pageNum: 1,
        pageSize: 30
      },
      totalNum: 0,
      newUserSwitch: false,
      systemValue: '',
      systemOptions: [{
        value: 'IOS',
        label: 'IOS'
      }, {
        value: 'ANDROID',
        label: 'ANDROID'
      }],
      dateValue: '',
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() > Date.now();
        }
      },
      tab: {
      },
      loading: false,
      tableData: [{
        date: '2019-02-12',
        registerNum: 0,
        loginNum: null,
        realnameNum: 0,
        realnameRate: 0
      }]
    }
  },
  methods: {
    // 仅看新用户开关
    newUserSwitchChange(val){
      console.log('val', val, this.newUserSwitch)
      this.getStatistic()
    },
    // 切换系统
    systemChange(val){
      console.log(this.systemValue)
    },
    renderHeader2(createElement, { column, _self }) {
      console.log(createElement) //  function (a, b, c, d) { return createElement(vm, a, b, c, d, true); };
      const label = column.label     
      const labelArr = label.split(' ') 
      console.log('labelArr', labelArr)
      return createElement(
        'span',     //创建最外层的标签可随意
        [
          createElement('span', {  // 创建第一个元素的标签可随意
            attrs: { type: 'text' } 
          },[labelArr[0]]), 
          createElement('p', {  // 创建第二个元素的标签可随意 
            attrs: { type: 'text', style: 'font-size:12px; font-weight:bold; color: #999999;' } // 给分割的某个元素单独加样式
          }, [labelArr[1], ' ', labelArr[2], ' ', labelArr[3]])
        ]
      )
    },
    renderHeader3(createElement, { column, _self }) {
      console.log(createElement) //  function (a, b, c, d) { return createElement(vm, a, b, c, d, true); };
      const label = column.label     
      const labelArr = label.split(' ') 
      console.log('labelArr', labelArr)
      return createElement(
        'span',     //创建最外层的标签可随意
        [
          createElement('span', {  // 创建第一个元素的标签可随意
            attrs: { type: 'text' } 
          },[labelArr[0]]), 
          createElement('p', {  // 创建第二个元素的标签可随意 
            attrs: { type: 'text', style: 'font-size:12px; font-weight:bold; color: #999999;' } // 给分割的某个元素单独加样式
          }, [labelArr[1], ' ', labelArr[2], ' ', labelArr[3], ' ', labelArr[4], ' ', labelArr[5]])
        ]
      )
    },
    renderHeader4(createElement, { column, _self }) {
      console.log(createElement) //  function (a, b, c, d) { return createElement(vm, a, b, c, d, true); };
      const label = column.label     
      const labelArr = label.split(' ') 
      console.log('labelArr', labelArr)
      return createElement(
        'span',     //创建最外层的标签可随意
        [
          createElement('span', {  // 创建第一个元素的标签可随意
            attrs: { type: 'text' } 
          },[labelArr[0]]), 
          createElement('p', {  // 创建第二个元素的标签可随意 
            attrs: { type: 'text', style: 'font-size:12px; font-weight:bold; color: #999999;' } // 给分割的某个元素单独加样式
          }, [labelArr[1], ' ', labelArr[2], ' ', labelArr[3], ' ', labelArr[4], ' ', labelArr[5], ' ', labelArr[6], ' ', labelArr[7]])
        ]
      )
    },
    handleSizeChange(val) {
      this.query.pageSize = val
      this.getStatistic()
    },
    handleCurrentChange(val) {
      this.query.pageNum = val
      this.getStatistic()
    },
    // 日期改变
    dateChange(val){
      console.log('dateValue', this.dateValue)
      this.getStatistic()
    },
    getStatistic(){
      let params = {
        isNewUser: this.newUserSwitch ? 1 : 0,
        pageSize: this.query.pageSize,
        pageNum: this.query.pageNum,
        startTime: !this.dateValue ? "" : this.dateValue[0],
        endTime: !this.dateValue ? "" : this.dateValue[1]
      }
      _statistics.getDaichaoTransfer(params)
      .then(res => {
        console.log('res', res.data)
        this.tableData = res.data.list || []
        this.tab.appLoginNumTotal = 0
        this.tab.appActiveNumTotal = 0
        this.tab.productUserNumTotal = 0
        this.tab.productPvNumTotal = 0
        this.tab.productUvNumTotal = 0
        this.tab.regisNumTotal = 0
        this.tab.luopanNumTotal = 0
        this.tab.bixiakuanNumTotal = 0
        this.tab.productUserRateAve = 0
        this.tab.chongfuRateAve = 0
        this.tab.productUvRateAve = 0
        this.tab.productUvRateTotal = 0
        this.tab.regisRateAve = 0
        this.tab.regisRateTotal = 0
        this.tab.luopanRateAve = 0
        this.tab.bixiakuanRateAve = 0
        this.tab.redRateAve = 0
        this.tab.redNumTotal= 0
        if(this.tableData.length > 0){
          this.tableData.forEach((item, index) => {
            this.tab.appLoginNumTotal += Number(item.appLoginNum)
            this.tab.appActiveNumTotal += Number(item.appActivationNum)
            this.tab.productUserNumTotal += Number(item.productUserNum)
            this.tab.productPvNumTotal += Number(item.productPvNum)
            this.tab.productUvNumTotal += Number(item.productUvNum)
            this.tab.regisNumTotal += Number(item.cpaNum)
            this.tab.luopanNumTotal += Number(item.listUvNum)
            this.tab.bixiakuanNumTotal += Number(item.mustUvNum)
            this.tab.redNumTotal += Number(item.redUvNum)
          })
          // 产品用户数比率
          if(isNaN((this.tab.productUserNumTotal*100/this.tab.appLoginNumTotal).toFixed(1))){
            this.tab.productUserRateAve = 0
          }else{
            this.tab.productUserRateAve = (this.tab.productUserNumTotal*100/this.tab.appLoginNumTotal).toFixed(1)
          }
          // 重复率
          if(isNaN((this.tab.productPvNumTotal*100/this.tab.productUvNumTotal).toFixed(1))){
            this.tab.chongfuRateAve = 0
          }else{
            this.tab.chongfuRateAve = (this.tab.productPvNumTotal*100/this.tab.productUvNumTotal).toFixed(1)
          }
          // 产品UV比率
          if(isNaN((this.tab.productUvNumTotal*100/this.tab.productUserNumTotal).toFixed(1))){
            this.tab.productUvRateAve = 0
          }else{
            this.tab.productUvRateAve = (this.tab.productUvNumTotal*100/this.tab.productUserNumTotal).toFixed(1)
          }
          // 产品UV总比率
          if(isNaN((this.tab.productUvNumTotal*100/this.tab.appLoginNumTotal).toFixed(1))){
            this.tab.productUvRateTotal = 0
          }else{
            this.tab.productUvRateTotal = (this.tab.productUvNumTotal*100/this.tab.appLoginNumTotal).toFixed(1)
          }
          // 预计注册比率
          if(isNaN((this.tab.regisNumTotal*100/this.tab.productUvNumTotal).toFixed(1))){
            this.tab.regisRateAve = 0
          }else{
            this.tab.regisRateAve = (this.tab.regisNumTotal*100/this.tab.productUvNumTotal).toFixed(1)
          }
          // 预计注册总比率
          if(isNaN((this.tab.regisNumTotal*100/this.tab.appLoginNumTotal).toFixed(1))){
            this.tab.regisRateTotal = 0
          }else{
            this.tab.regisRateTotal = (this.tab.regisNumTotal*100/this.tab.appLoginNumTotal).toFixed(1)
          }
          // 贷款罗盘比率
          if(isNaN((this.tab.luopanNumTotal*100/this.tab.appLoginNumTotal).toFixed(1))){
            this.tab.luopanRateAve = 0
          }else{
            this.tab.luopanRateAve = (this.tab.luopanNumTotal*100/this.tab.appLoginNumTotal).toFixed(1)
          }
          // 必下款比率
          if(isNaN((this.tab.bixiakuanNumTotal*100/this.tab.appLoginNumTotal).toFixed(1))){
            this.tab.bixiakuanRateAve = 0
          }else{
            this.tab.bixiakuanRateAve = (this.tab.bixiakuanNumTotal*100/this.tab.appLoginNumTotal).toFixed(1)
          }
          // 抢红包比率
          if(isNaN((this.tab.redNumTotal*100/this.tab.appLoginNumTotal).toFixed(1))){
            this.tab.redRateAve = 0
          }else{
            this.tab.redRateAve = (this.tab.redNumTotal*100/this.tab.appLoginNumTotal).toFixed(1)
          }
        }
        this.totalNum = res.data.total
      })
    },
    // 刷新按钮
    refreshClick(){
      this.getStatistic()
    }
  },
  created(){
    this.dateValue = [this._mapping.date_format2(new Date()), this._mapping.date_format2(new Date())]
    this.getStatistic()
  }
}
</script>
<style lang="scss" scoped>
</style>